{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']=False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf=pd.read_csv('ab_data.csv')\n",
    "pf['date'] =pf.timestamp.str[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017/1/21 22:11:49</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017/1/12 08:01:45</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017/1/11 16:55:06</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017/1/8 18:28:03</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/8 1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017/1/21 01:52:26</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "      <td>2017/1/21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294473</th>\n",
       "      <td>751197</td>\n",
       "      <td>2017/1/3 22:28:39</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/3 2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294474</th>\n",
       "      <td>945152</td>\n",
       "      <td>2017/1/12 00:51:57</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294475</th>\n",
       "      <td>734608</td>\n",
       "      <td>2017/1/22 11:45:03</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294476</th>\n",
       "      <td>697314</td>\n",
       "      <td>2017/1/15 01:20:29</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>294477</th>\n",
       "      <td>715931</td>\n",
       "      <td>2017/1/16 12:40:24</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017/1/16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>294478 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        user_id           timestamp      group landing_page  converted  \\\n",
       "0        851104  2017/1/21 22:11:49    control     old_page          0   \n",
       "1        804228  2017/1/12 08:01:45    control     old_page          0   \n",
       "2        661590  2017/1/11 16:55:06  treatment     new_page          0   \n",
       "3        853541   2017/1/8 18:28:03  treatment     new_page          0   \n",
       "4        864975  2017/1/21 01:52:26    control     old_page          1   \n",
       "...         ...                 ...        ...          ...        ...   \n",
       "294473   751197   2017/1/3 22:28:39    control     old_page          0   \n",
       "294474   945152  2017/1/12 00:51:57    control     old_page          0   \n",
       "294475   734608  2017/1/22 11:45:03    control     old_page          0   \n",
       "294476   697314  2017/1/15 01:20:29    control     old_page          0   \n",
       "294477   715931  2017/1/16 12:40:24  treatment     new_page          0   \n",
       "\n",
       "              date  \n",
       "0       2017/1/21   \n",
       "1       2017/1/12   \n",
       "2       2017/1/11   \n",
       "3       2017/1/8 1  \n",
       "4       2017/1/21   \n",
       "...            ...  \n",
       "294473  2017/1/3 2  \n",
       "294474  2017/1/12   \n",
       "294475  2017/1/22   \n",
       "294476  2017/1/15   \n",
       "294477  2017/1/16   \n",
       "\n",
       "[294478 rows x 6 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 检验指标确定（10分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "一类指标（一定不能下降的指标）：人均停留时长\n",
    "\n",
    "二类指标：banner点击率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 确定检验统计量  （5分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "一类指标统计量：A、B组人均停留时长均值之差\n",
    "\n",
    "二类指标统计量：A、B组banner点击率之差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 埋点收集数据  （10分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 确定H0,H1 （10分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 一类指标：\n",
    "\n",
    "#### H0:\n",
    "\n",
    "$$\n",
    "stime_A-stime_B>=2*std_A\n",
    "$$\n",
    "\n",
    "#### H1:\n",
    "\n",
    "$$\n",
    "stime_A-stime_B<2*std_A\n",
    "$$\n",
    "\n",
    "### 二类指标：\n",
    "#### H0:\n",
    "\n",
    "​\t\t\t\t\t\t\t\t\n",
    "$$\n",
    "p_B-p_A<=0\n",
    "$$\n",
    "\n",
    "#### H1:\n",
    "\n",
    "​\t\t\t\t\t\t\t\n",
    "$$\n",
    "p_B-p_A>0\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 确定显著水平α  （5分)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "一类错误使用默认值α=0.05\n",
    "\n",
    "二类错误使用默认值β=0.2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算样本量 （15分）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一类指标（不计算）为估计两个总体的均值之差，样本量计算公式：\n",
    "$$\n",
    "n_A=kn_b and n_b=(1+\\frac{1}{k})(\\sigma\\frac{z_{1-\\alpha}+z_{1-\\beta}}{\\mu_A-\\mu_B})^2\n",
    "$$\n",
    "二类指标为估计两个总体比例之差，样本量计算公式\n",
    "$$\n",
    "n=p_0(1-p_0)(\\frac{z_{1-\\alpha}+z_{1-\\beta}\\sqrt{\\frac{p(1-p)}{p_0(1-p_0)}}}{p-p_0})^2\n",
    "$$\n",
    "其中𝑝𝐵为𝑝,𝑝𝐴为𝑝0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算p\n",
    "p_A = pf.converted[(pf.group=='control')&(pf.landing_page=='old_page')].mean()\n",
    "p_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10589344218918344"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=p_A*(1-p_A)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6448536269514729"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算𝑧1−𝛼\n",
    "alpha =0.05\n",
    "z1_a =stats.norm.isf(alpha,loc=0,scale=1)\n",
    "z1_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8416212335729142"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算𝑧1−𝛽\n",
    "beta =0.2\n",
    "z1_b=stats.norm.isf(beta,loc=0,scale=1)\n",
    "z1_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#经验设定𝑝𝐵−𝑝𝐴=0.01\n",
    "p_B=p_A+0.01\n",
    "b=p_B*(1-p_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6701.938803160933"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n=a*((z1_a+z1_b*np.sqrt(b/a))/0.01)**2\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "landing_page\n",
       "new_page    147239\n",
       "old_page    147239\n",
       "Name: user_id, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pf.groupby(['landing_page'])['user_id'].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "打分标准：\n",
    "    1   读取ab_test.csv 数据  2分（上一部分已完成）\n",
    "    2   计算统计量  3分（上一部已完成）\n",
    "    3   计算P值正确 10分\n",
    "    4   决策正确 5分\n",
    "    5   封装成功 10分  有输入输出参数并能成功调用\n",
    "    6    封装的健壮性， 按 P值计算的判断条件完整性计算 每个条件为 5分 共15分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>control</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0.121369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0.120386</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0.118807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>treatment</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0.127226</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       group landing_page  converted\n",
       "0    control     new_page   0.121369\n",
       "1    control     old_page   0.120386\n",
       "2  treatment     new_page   0.118807\n",
       "3  treatment     old_page   0.127226"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pf.groupby(['group','landing_page'],as_index = False)['converted'].mean()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "stat = df.converted[2]-df.converted[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
